#include "GameUi.h"
#include "JumpController.h"
#include <SFML/Graphics.hpp>

#include <iostream>
#include <iomanip>
#include <sstream>

#define _USE_MATH_DEFINES
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265359
#endif

class JumpController;

GameUi::GameUi(GameState *gameState, sf::RenderWindow *window) : GameObject(gameState)
{
  addProperty("gameui");
  m_window = window;

  if (!m_font.loadFromFile("mono.ttf"))
  {
    std::cerr << "no s'ha pogut carregar la font" << std::endl;
  }

  m_text.setFont(m_font);
  m_text.setCharacterSize(18);
  m_text.setColor(sf::Color::Black);
  m_text.setPosition(200,400);

  m_textLevel.setFont(m_font);
  m_textLevel.setCharacterSize(56);
  m_textLevel.setColor(sf::Color::Black);
  m_textLevel.setPosition(200,0);
  m_textLevel.setStyle(sf::Text::Bold);

  m_textSuccess.setFont(m_font);
  m_textSuccess.setCharacterSize(40);
  m_textSuccess.setColor(sf::Color::Red);
  m_textSuccess.setPosition(320,100);
  m_textSuccess.setStyle(sf::Text::Bold);
  m_textSuccess.setString("Success!");
}

GameUi::~GameUi()
{
}

void GameUi::update(double dt)
{
  std::stringstream ss;
  JumpController *jc = m_gameState->getJumpController();
  double angle = 0.0;
  double speed = 0.0;
  int a, s;
  if (jc != 0)
  {
    angle = jc->getAngle();
    speed = jc->getSpeed();
  }

  angle = angle * 180.0 / M_PI;
  speed = speed * 100.0;
  a = (int) angle;
//  s = (( (int) speed) / 10) * 10;
  s = (int) speed;

  ss << "angle" << std::setw(4) << a << std::endl << "speed"  << std::setw(4) << s;
  m_text.setString(ss.str());

  ss.str("");
  ss.clear();

  


  if(m_gameState->getCurrentLevel() >= m_gameState->getMaxLevels()){
	  m_textSuccess.setPosition(250,100);
	  m_textSuccess.setString("Thanks for playing!");
  }
  
  if(m_gameState->getCurrentLevel() <= m_gameState->getMaxLevels()){
	  ss << "Level " << m_gameState->getCurrentLevel() << "/" << m_gameState->getMaxLevels() << std::endl;
	  m_textLevel.setString(ss.str());
  }
}


void GameUi::draw()
{
  m_window->pushGLStates();
  m_window->draw(m_text);

  sf::FloatRect textRect = m_textLevel.getLocalBounds();
  m_textLevel.setOrigin(textRect.left + textRect.width/2.0f, textRect.top + textRect.height/2.0f);
  m_textLevel.setPosition(sf::Vector2f(400.0f, 56.0f));

  m_window->draw(m_textLevel);

  if(m_gameState->m_levelCompleted)
	  m_window->draw(m_textSuccess);
  m_window->popGLStates();
}
